#include <stdio.h>
#include <string.h>
#include "fdb_port.h"
#include "bsp_log.h"
#include "flashdb.h"
#include "flashdb_api_demo.h"
#include "rtc.h"  

#define DEMO_NAME "FLASDB_DEMO"
#define FLASH_LOG_INFO(fmt, ...)  BSP_LOG(LOG_INFO,"%s %s-%d "fmt,DEMO_NAME,__FUNCTION__,__LINE__, ##__VA_ARGS__)
#define FLASH_LOG_DEBUG(fmt, ...) BSP_LOG(LOG_DEBUG, "%s %s-%d "fmt,DEMO_NAME,__FUNCTION__, __LINE__,##__VA_ARGS__)
#define FLASH_LOG_ERROR(fmt, ...) BSP_LOG(LOG_ERROR, "%s %s-%d "fmt,DEMO_NAME,__FUNCTION__, __LINE__,##__VA_ARGS__)

static void kvdb_blob_make_api_demo(fdb_blob_t blob,const void* data,size_t data_len)
{
	blob = fdb_blob_make(blob,data, data_len);
	if(blob->size)
	{
		FLASH_LOG_INFO("create blob success! blob->size:%d !\r\n",blob->size);
	}else{
		FLASH_LOG_ERROR("create blob fail!\r\n");
	}
}
static fdb_kvdb_t init_kvdb()
{
	fdb_kvdb_t db = get_kvdb();
	fdb_err_t ret = fdb_kvdb_init(db, "w25q128", "fdb_kvdb1", NULL,NULL);
	if(!ret)
	{
		FLASH_LOG_INFO("init kvdb success ret:%d \r\n",ret);
	}else{
		FLASH_LOG_ERROR("init kvdb error ret:%d \r\n",ret);
		return NULL;
	}
	return db;
}

/**
 * @brief 获取当前时间戳demo测试
 *
 * FlashDB TSDB 需要一个时间戳，单位秒。
 * 可以改成读取 RTC、系统 tick 等。
 */
static fdb_time_t get_time(void) {
    RTC_DateTypeDef sDate;
    RTC_TimeTypeDef sTime;
    struct tm timeinfo;

    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

    timeinfo.tm_year = sDate.Year + 100;   // 偏移到 2000-2099
    timeinfo.tm_mon  = sDate.Month - 1;
    timeinfo.tm_mday = sDate.Date;
    timeinfo.tm_hour = sTime.Hours;
    timeinfo.tm_min  = sTime.Minutes;
    timeinfo.tm_sec  = sTime.Seconds;

    return (fdb_time_t)mktime(&timeinfo);
}
static fdb_tsdb_t tsdb_init()
{
	fdb_err_t ret;
	fdb_tsdb_t tsdb = get_tsdb();
	ret =  fdb_tsdb_init(tsdb, "w25q128", "fdb_tsdb1",get_time, 128, NULL);
	if(ret == 0)
	{
		FLASH_LOG_INFO("fdb_tsdb_init success\r\n");
		return tsdb;
	}
	return NULL;
}

void demo(void)
{
	fdb_err_t ret = -1;
	fdb_kvdb_t db = init_kvdb();
	if(!db)
	{
		FLASH_LOG_ERROR("kv_db init error\r\n");
	}
//	int temp_data = 36;
//	struct fdb_blob blob;
//	kvdb_blob_make_api_demo(&blob,(const void *)&temp_data,sizeof(temp_data));
//	//blob 存储
//	ret = fdb_kv_set_blob(db, "tmp", &blob);
//	if(!ret)
//	{
//		FLASH_LOG_INFO("fdb_kv_set_blob success\r\n");
//	}
//	//blob 读取
//	int read_temp_data = 0;
//	struct fdb_blob read_blob;
//	kvdb_blob_make_api_demo(&read_blob,(const void *)&read_temp_data,sizeof(read_temp_data));
//	size_t read_len = fdb_kv_get_blob(db, "tmp", &read_blob);
//	if(read_len > 0)
//	{
//		FLASH_LOG_INFO("fdb_kv_get_blob success read_temp_data:%d\r\n",read_temp_data);
//	}
	
	const char *key = "stm32";
	const char *value = "hello world";
	//Sting 类型 写入
	ret = fdb_kv_set(db, key, value);
	if(!ret)
	{
		FLASH_LOG_INFO("String value set success!\r\n");
	}
	
	char *read_value = fdb_kv_get(db,key);
	if(strlen(read_value) != 0)
	{
		FLASH_LOG_ERROR("String value get success value:%s!\r\n",read_value);
	}
	ret = fdb_kv_del(db,key);
	if(!ret)
	{
		FLASH_LOG_INFO("del key:%s success!\r\n",key);
	}
	char *read_value_t = fdb_kv_get(db,key);
	if(!read_value_t)
	{
		FLASH_LOG_ERROR("String value get error!\r\n");
	}
}


struct sensor_data {
    int temperature;    // 温度
    int humidity;       // 湿度
};
void tsdb_demo_write(fdb_tsdb_t tsdb,int temp, int humi) {
    struct sensor_data data;

    data.temperature = temp;
    data.humidity = humi;
	struct fdb_blob blob;
	kvdb_blob_make_api_demo(&blob,(const void *)&data,sizeof(data));
    fdb_err_t result = fdb_tsl_append(tsdb, &blob);
    if (result == FDB_NO_ERR) {
        FLASH_LOG_INFO("fdb_tsl_append: temp=%d, humi=%d\r\n", temp, humi);
    } else {
        FLASH_LOG_ERROR("fdb_tsl_append error！\r\n");
    }
}

static bool query_cb(fdb_tsl_t tsl, void *arg)
{
    struct fdb_blob blob;
    struct sensor_data status;
    fdb_tsdb_t db = arg;
    fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
    FLASH_LOG_INFO("[query_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temperature, status.humidity);
    return false;
}
void tsdb_demo_query(fdb_tsdb_t tsdb) {
    printf("开始遍历 TSDB 数据...\r\n");
    fdb_tsl_iter(tsdb, query_cb, NULL);
    printf("遍历结束\r\n");
}
void tsdb_demo(void)
{

	fdb_tsdb_t tsdb = tsdb_init();
	tsdb_demo_write(tsdb,1,1);
	tsdb_demo_query(tsdb);
}
	



